package concurrent;

import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierTest {

    static class Worker implements Runnable {
        private final CyclicBarrier startSignal;
        Worker(CyclicBarrier startSignal) {
            this.startSignal = startSignal;
        }
        public void run() {
            while (true) {
                try {
                    startSignal.await();
                    doWork();
                } catch (Exception ex) {} // return;
            }
        }

        void doWork() { 
            try{
                Thread.sleep(300);
            }catch(InterruptedException e){
            }
            System.out.println("worker done :" + startSignal.getNumberWaiting());
        }
    }
    /**
     * @param args
     *
     * @author grind
     * @date Mar 14, 2014 11:15:28 AM
     */
    public static void main(String[] args) {
        try {
            CyclicBarrier startSignal = new CyclicBarrier(1);
            System.out.println("main1 :" + startSignal.getNumberWaiting());

    
            new Thread(new Worker(startSignal)).start();
            Thread.sleep(300);

        } catch (Exception ex) {}
        
    }

}
